<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetBarDataApi} from "@/api"
import type {Bar} from "@/interface"

const bar = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const barData = ref<Bar>({
  name: [],
  data: []
})
const init = async () => {
  const {data: res} = await useGetBarDataApi()
  barData.value = res
  myChart.value = markRaw(echarts.init(bar.value));
  draw();
}
const draw = () => {
  option.value = {
    title: {
      text: '2023年11月电影总票房top10',
      left: 'center',
      textStyle: {
        color: '#000000'
      }
    },
    grid: {
      bottom: 120
    },
    tooltip: {},
    xAxis: {
      type: 'category',
      data: barData.value.name,
      axisLabel: {
        rotate: 45,
        color: '#000000'
      },
      axisLine: {
        lineStyle: {
          color: '#000000'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        color: '#000000'
      },
      splitLine: {
        lineStyle: {
          color: "#000000"
        }
      }
    },
    series: [
      {
        name: '总票房(单位：万)',
        data: barData.value.data,
        type: 'bar'
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="bar" class="bar">
  </div>
</template>

<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>